<?php
/*
	提现
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
global $_RQ;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return['ischecklogin']=$this->model('member')->checklogin();
if($_RQ['op']=='get'){
	$this->model('team')->update_agent_level($_SESSION['uid']);
	$teamsetting=$this->plugin_setting('teamsetting');
	$basic=model('core/member')->basic($_SESSION['uid']);
	$item=pdo_get('jy_robshop_team',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$mymember=pdo_get('jy_robshop_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$level=pdo_get('jy_robshop_teamlevel',array('id'=>$item['level_id'],'uniacid'=>$_SESSION['uniacid']));
	$member=array_merge($basic,$item);
	$return['member']=$member;
	$return['level']=$level;
	$teamids=$this->model('team')->getteamids($item['mid']);
	$mymember['teamnum']=count($teamids);
	if($teamids){
		$mymember['order_price']=pdo_getsum('jy_robshop_canlog',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$teamids),'fee');
		$mymember['team_zfee']=pdo_getsum('jy_robshop_canlog',array('ispay'=>1,'status'=>2,'uniacid'=>$_SESSION['uniacid'],'mid'=>$teamids),'fee');
	}
	$mymember['my_zfee']=pdo_getsum('jy_robshop_canlog',array('ispay'=>1,'status'=>2,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'fee');
	$mymember['getfee']=pdo_getsum('jy_robshop_commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'money');
	$mymember['getfee1']=pdo_getsum('jy_robshop_commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>1),'money');
	$mymember['getfee2']=pdo_getsum('jy_robshop_commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>2),'money');
	$mymember['getfee3']=pdo_getsum('jy_robshop_commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>3),'money');
	$mymember['getfee4']=pdo_getsum('jy_robshop_commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>4),'money');
	$mymember['getfee5']=pdo_getsum('jy_robshop_commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>5),'money');
	$mymember['isdui']=pdo_getsum('jy_robshop_balance',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1),'money');
	$return['mymember']=$mymember;
	$teampage=$this->plugin_setting('teampage');
	$return['teampage']=$teampage;
	$return['params']['type']=$_RQ['type']?:2;
	//结算
	$return['setting']['balance_money']=$teamsetting['balance_money'];
	$data=array();
	$data['commission_total']=$this->sum('commission',array('mid'=>$_SESSION['uid']),'money');
	$data['commission_0']=$this->sum('commission',array('mid'=>$_SESSION['uid'],'status'=>0),'money');
	//待结算佣金
	$data['commission_1']=$this->sum('commission',array('mid'=>$_SESSION['uid'],'status'=>1),'money');
	//可结算佣金
	$params=array('mid'=>$_SESSION['uid'],'status'=>1);
	if($teamsetting['balance_day']){
		//结算天数
		$time=TIMESTAMP-intval($teamsetting['balance_day'])*86400;
		$params['complete_time <']=$time;
	}
	$data['commission_1_1']=$this->sum('commission',$params,'money');
	$data['balance_total']=$this->sum('balance',array('mid'=>$_SESSION['uid']),'money');
	$data['balance_1']=$this->sum('balance',array('mid'=>$_SESSION['uid'],'status'=>1),'money');
	$data['balance_2']=$this->sum('balance',array('mid'=>$_SESSION['uid'],'status'=>2),'money');
	$data['balance_3']=$this->sum('balance',array('mid'=>$_SESSION['uid'],'status'=>3),'money');
	$params=array('mid'=>$_SESSION['uid'],'status'=>1);
	$params['type']=5;
	$data['daili_fee']=$this->sum('commission',$params,'money');
	$params['type']=6;
	$data['pingfee_fee']=$this->sum('commission',$params,'money');
	$data['service_money']=intval($teamsetting['balance_per']*$data['commission_1_1'])/100;
	$data['cash_money']=$data['commission_1_1']-$data['service_money'];
	foreach($data as &$row){
		$row=price($row);
	}
	$return['data']=$data;
	if($teamsetting['withdrawtype']){
		$withdrawtype=array();
		$check=strstr($teamsetting['withdrawtype'],'credit');
		if($check){
			$withdrawtype['credit']=$this->pay_types['credit'];
			$pay_type='credit';
		}
		$check=strstr($teamsetting['withdrawtype'],'wechat');
		if($check){
			$withdrawtype['wechat']=$this->pay_types['wechat'];
			$pay_type=$pay_type?:'wechat';
		}
		$check=strstr($teamsetting['withdrawtype'],'alipay');
		if($check){
			$withdrawtype['alipay']=$this->pay_types['alipay'];
			$pay_type=$pay_type?:'alipay';
		}
		$check=strstr($teamsetting['withdrawtype'],'union');
		if($check){
			$withdrawtype['union']=$this->pay_types['union'];
			$pay_type=$pay_type?:'union';
		}
		$return['pay_types']=$withdrawtype;
	}else{
		$return['pay_types']=$this->pay_types;
		$pay_type='credit';
	}
	$wxapppay_types=array();
	foreach($return['pay_types'] as $k=>$pv){
		$wxapppay_types[]=array(
			'key'=>$k,
			'title'=>$pv,
		);
	}
	$return['wxapppay_types']=$wxapppay_types;
	$return['post']=array('pay_type'=>$pay_type);
	set_html('title','我的团队');
	exi($return);
}
if($_RQ['op']=='getlist'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if($_RQ['type']){
		$params['type']=$_RQ['type'];
	}
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_robshop_commission',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
			$v['member']=cfc('core')->member_show($v['buymid']);
			$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
			$v['statustitle']=$this->commission_status[$v['status']];
			$v['typetitle']=$this->commission_money_types[$v['type']];
	}
	exi($return);
}
//结算
if($_RQ['op']=='balance'){
	$post=get_postdata();
	$this->model('commission')->create_balance(0,$post);
	exi('结算成功！','','/team.index/');
}